package com.six.controller;

import com.six.entity.MinioBean;
import com.six.utils.MinioUtil;
import io.minio.Result;
import io.minio.errors.*;
import io.minio.messages.DeleteError;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * ClassName:MinioController
 * Package:com.six.controller
 * Description
 *
 * @Author:@wenxueshi
 * @Create:2023/7/3 - 22:39
 * @Version:v1.0
 */
@Api(tags = "Minio测试")
@RestController
public class MinioController {
    @Autowired
    private MinioUtil minioUtil;

    //多文件上传
    @PostMapping("/upload")
    public Object upload(MultipartFile[] file) {
        List<String> upload = minioUtil.upload(file);
        return upload;
    }

    //判断bucket是否存在
    @GetMapping("/exist")
    public boolean exists(@RequestParam("bucketName") String bucketName){
        return minioUtil.existBucket(bucketName);
    }

    //创建存储bucketName
    @PostMapping("/create")
    public boolean makeBucket(@RequestParam("bucketName") String bucketName){
        return minioUtil.makeBucket(bucketName);
    }

    //删除存储bucketName
    @DeleteMapping("/delete")
    public boolean deleteBucket(@RequestParam("bucketName") String bucketName){
        return minioUtil.removeBucket(bucketName);
    }

    @PostMapping("/download")
    public Object download(@RequestParam("fileName") String fileName){
        return minioUtil.download(fileName);
    }

    //查看文件对象
    @ApiOperation(value = "查看文件对象")
    @GetMapping("/list")
    public List<MinioBean> list(@RequestParam("bucketName") String bucketName){
        return minioUtil.listObjects(bucketName);
    }

    //批量删除文件对象
    @ApiOperation(value = "批量删除文件对象")
    @DeleteMapping("/batchDelete/{bucketName}")
    public boolean batchDeleteBucket(@PathVariable("bucketName") String bucketName, @RequestParam List<String> fileNames) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return minioUtil.batchDeleteBucket(bucketName,fileNames);
    }

    //删除指定文件
    @DeleteMapping("/delete/{bucketName}/{fileName}")
    public boolean delete(@PathVariable("bucketName") String bucketName,@PathVariable("fileName") String fileName){
        return minioUtil.deleteFile(bucketName,fileName);
    }
}
